package com.hzo.great.leisuregreat.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzo.great.leisuregreat.R;
import com.hzo.great.leisuregreat.adapters.PhoneAllListAdapter;
import com.hzo.great.leisuregreat.beans.PhoneBean;
import com.hzo.great.leisuregreat.beans.PhoneListDataBean;
import com.hzo.great.leisuregreat.constants.UrlConstant;
import com.hzo.great.leisuregreat.dialog.LoadingDialog;
import com.hzo.great.leisuregreat.utils.MyStringCallback;
import com.hzo.great.leisuregreat.utils.RequestSever;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeFragment extends Fragment {

    private RecyclerView listPhone;
    private PhoneAllListAdapter mAdapter;
    private Context mContext;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        View view = inflater.inflate(R.layout.home_fragment, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        listPhone = view.findViewById(R.id.list_view);
        //设置RecyclerView管理器
        listPhone.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));

        getData();
    }

    private void getData() {
        LoadingDialog.getInstance(getActivity()).showLoadDialog("");
        RequestSever.post(UrlConstant.HOME_URL, getHomeMap(), new MyStringCallback<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                JSONObject jsonObject = JSON.parseObject(result);
                int code = jsonObject.getIntValue("code");
                switch (code) {
                    case 200:
                        List<PhoneListDataBean> phoneAllList = new ArrayList<>();
                        JSONArray dataArray = jsonObject.getJSONArray("data");
                        for (int i = 0; i < dataArray.size(); i++) {
                            PhoneListDataBean phoneListDataBean = new PhoneListDataBean();
                            JSONObject object = dataArray.getJSONObject(i);
                            phoneListDataBean.setKey(object.getString("Key"));

                            List<PhoneBean> phoneList = new ArrayList<>();
                            JSONArray phoneListArray = object.getJSONArray("PhoneList");
                            for (int j = 0; j < phoneListArray.size(); j++) {
                                JSONObject phoneJson = phoneListArray.getJSONObject(j);
                                PhoneBean phoneBean
                                        = PhoneBean.JSONStrToJavaBeanObj(phoneJson.toString());
                                phoneList.add(phoneBean);
                            }
                            phoneListDataBean.setPhoneList(phoneList);

                            phoneAllList.add(phoneListDataBean);
                        }

                        //初始化适配器
                        mAdapter = new PhoneAllListAdapter(phoneAllList, mContext);
                        listPhone.setAdapter(mAdapter);
                        break;
                }
                LoadingDialog.closeLoadDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                LoadingDialog.closeLoadDialog();
            }
        });
    }

    private Map<String, Object> getHomeMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("Pageindex", 1);
        return map;
    }
}
